package cn.wolfcode.luowowo.article.Service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.mapper.RegionMapper;
import cn.wolfcode.luowowo.article.query.DestinationQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService{
    @Autowired
    private DestinationMapper destinationMapper;
    @Autowired
    private RegionMapper regionMapper;

    public List<Destination> getDestByDeep(int deep) {
        return destinationMapper.selectByDeep(deep);

    }

    public List<Destination> getDestByRegionId(Long rid) {
        //需要执行一个分支，国内分支
        if(rid == -1){
            return destinationMapper.selectDestByParentId(1L);
        }

        Region region = regionMapper.selectByPrimaryKey(rid);
        //某个地区下关联的目的地id
        Long[] refIds = region.getRefIds();

        // select from deatination where id in (refIds)
        return destinationMapper.getDestByRegionId(refIds);
    }

    public PageInfo query(DestinationQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Destination> list = destinationMapper.selectForList(qo);

        return new PageInfo(list);
    }

    public void changeHotValue(Boolean hot, Long id) {
        destinationMapper.changeHotValue(hot,id);
    }

    public void setInfo(String info,Long id) {
        destinationMapper.setInfo(info,id);
    }

    public List<Destination> getToasts(Long parentId) {
        List<Destination> list = new ArrayList<>();

        createToasts(list,parentId);
        //反转位置
        Collections.reverse(list);
        return list;
    }

    public List<Destination> listAll() {

        return destinationMapper.selectAll();
    }

    public Boolean isAbord(Long id) {
        Destination destination = destinationMapper.selectByPrimaryKey(id);

        if(destination == null){
            return false;
        }
        if(destination.getName().equals("中国")){
            return false;
        }
        if(destination.getParent() != null){
            if(destination.getParent().getName().equals("中国")){
                return false;
            }
            return isAbord(destination.getParent().getId());
        }
        return true;

    }

    private void createToasts(List<Destination> list, Long parentId) {
        Destination destination = destinationMapper.selectByPrimaryKey(parentId);
        if(destination == null){
            return;
        }
        list.add(destination);

        if(destination.getParent() != null){
            createToasts(list,destination.getParent().getId());
        }
    }

    public Destination getCountry(Long id) {
        Destination dest = destinationMapper.selectByPrimaryKey(id);
        if(dest.getParent() == null ){
            return dest;
        }
        List<Destination>toasts = this.getToasts(id);
        if(toasts != null && toasts.size() >0){
            return toasts.get(0);
        }
        return null;
    }

    public Destination getProvince(Long id) {
        if(this.isAbord(id)){
            return null;
        }
        Destination dest = destinationMapper.selectByPrimaryKey(id);
        if(dest.getDeep() == 1 ){
            return null;
        }
        if(dest.getDeep() == 2){
            return dest;
        }
        if(dest.getDeep() ==3){
            return dest.getParent();
        }
        return null;
    }

    public List<Destination> queryDestByDeep(int i) {
        return destinationMapper.selectByDeep(i);
    }
}
